package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.xca.znb.R;
import com.xca.znb.entity.Goods;
import com.xca.znb.entity.Local;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.ClearingImpl;
import com.xca.znb.presenter.ClearingPresenter;

public class ClearingActivity extends AppCompatActivity {
    private ImageButton Back , chooseLocal , minusBtn , addBtn;
    private Button Submit ;
    private TextView local , bName , bId , sName , gName , gPrice , gUnit , bNum ,
    bMoney , gFreight , pMoney ;
    private EditText iNum , iRemark ;
    private ImageView gImg , sImg ;

    private ClearingPresenter presenter ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clearing);
        initView();

        ClearingImpl clearingImpl = new ClearingImpl(this);
        presenter = new ClearingPresenter(clearingImpl);


        Intent getMsg = getIntent();
        int goodsId = getMsg.getIntExtra("payGId" , 1);
        int localId = getMsg.getIntExtra("payLId" , 1);
        String remark = getMsg.getStringExtra("payRemark");
        int num = getMsg.getIntExtra("payNum" , 1);

        if (remark == null){
            iRemark.setHint("选填：对本次交易的说明");
        }else {
            iRemark.setText(remark);
        }

        SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

//        在这里进行信息的展示
        Goods goods = presenter.getGoodsById(goodsId);
        User sales = presenter.getUserByPhone(goods.getPublisher());
        Glide.with(this)
                        .load(sales.getConcern())
                                .into(sImg);
        if (sales.getName()!=null){
            sName.setText(sales.getName());
        }else {
            sName.setText(goods.getPublisher());
        }

        Glide.with(this)
                        .load(goods.getFirst_image())
                                .into(gImg);

        gName.setText(goods.getName());
        gPrice.setText(String.valueOf(goods.getPrice()));
        gUnit.setText(goods.getUnit());
        gFreight.setText(goods.getFreight());
        iNum.setText(String.valueOf(num));

        pMoney.setText(String.valueOf(goods.getPrice() * Integer.parseInt(iNum.getText().toString())));
        bMoney.setText(String.valueOf(goods.getPrice() * Integer.parseInt(iNum.getText().toString())));

//        对数量输入框进行监听
        iNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s!=null && !s.toString().isEmpty()){
                    int num = Integer.parseInt(iNum.getText().toString());
                    pMoney.setText(String.valueOf(goods.getPrice() * num));
                    bMoney.setText(String.valueOf(goods.getPrice() * num));
                }else {
                    pMoney.setText("0");
                    bMoney.setText("0");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        //增加按钮和减少按钮
        minusBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = iNum.getText().toString();
                int number = Integer.parseInt(num);
                if (number>1){
                    iNum.setText(String.valueOf(number-1));
                    bMoney.setText(String.valueOf(goods.getPrice() * (number-1)));
                    pMoney.setText(String.valueOf(goods.getPrice() * (number-1)));
                }else {
                    Toast.makeText(ClearingActivity.this, "亲，不能再少了", Toast.LENGTH_SHORT).show();
                }
            }
        });
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = iNum.getText().toString();
                int number = Integer.parseInt(num);
                iNum.setText(String.valueOf(number+1));
                bMoney.setText(String.valueOf(goods.getPrice() * (number+1)));
                pMoney.setText(String.valueOf(goods.getPrice() * (number+1)));
            }
        });



        Local getLocal = presenter.getLocalById(localId);
        if (local != null){
            local.setText(getLocal.getAddress());
            bName.setText(getLocal.getName());
            bId.setText(getLocal.getPhone());
        }else {
            local.setText("请创建地址");
        }



        chooseLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String stringNum = iNum.getText().toString();
                if (!TextUtils.isEmpty(stringNum)){
                    Intent intent = new Intent(ClearingActivity.this , LocalPayActivity.class);
                    intent.putExtra("tcGLocal" , goodsId);
                    intent.putExtra("tcLLocal",localId);
                    String inputRemark = iRemark.getText().toString().trim();
                    intent.putExtra("tcRLocal" , inputRemark);

                    int inputNum =  Integer.parseInt(iNum.getText().toString());
                    if (inputNum<1){
                        Toast.makeText(ClearingActivity.this, "商品数量不规范", Toast.LENGTH_SHORT).show();
                    }else {
                        intent.putExtra("tcNLocal",inputNum);
                        startActivity(intent);
                        finish();
                    }
                }else {
                    Toast.makeText(ClearingActivity.this, "请输入购买数量", Toast.LENGTH_SHORT).show();
                }
            }
        });

        iNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                bNum.setText(s);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String getINum = iNum.getText().toString().trim();
                if (!TextUtils.isEmpty(getINum)){
                    Intent intent = new Intent(ClearingActivity.this , EndPayActivity.class);
                    intent.putExtra("payLocalId" , localId);
                    intent.putExtra("payGoodsId" , goodsId);
                    intent.putExtra("payNum" , Integer.parseInt(getINum));
                    String pRemark = iRemark.getText().toString().trim();
                    intent.putExtra("payRemark" , pRemark);
                    startActivity(intent);
                }
            }
        });
    }

    private void initView(){
        Back = findViewById(R.id.clearing_back) ;
        Submit = findViewById(R.id.clearing_submit) ;
        chooseLocal = findViewById(R.id.clearing_choose_local);
        minusBtn = findViewById(R.id.clearing_minus_one);
        addBtn = findViewById(R.id.clearing_add_one);
        gImg = findViewById(R.id.clearing_goods_img);
        sImg = findViewById(R.id.clearing_sales_img);
        iNum = findViewById(R.id.clearing_show_number);
        iRemark = findViewById(R.id.clearing_order_remark);
        local = findViewById(R.id.detail_local_clearing);
        bName = findViewById(R.id.clearing_user_name);
        bId = findViewById(R.id.clearing_user_id);
        sName = findViewById(R.id.clearing_store_name);
        gName = findViewById(R.id.clearing_goods_name);
        gPrice = findViewById(R.id.clearing_goods_price);
        gUnit = findViewById(R.id.clearing_goods_unit);
        gFreight = findViewById(R.id.clearing_goods_freight);
        pMoney = findViewById(R.id.clearing_need_pay);
        bMoney = findViewById(R.id.clearing_need_money);
        bNum = findViewById(R.id.clearing_buy_goods_number);
    }
}
